package al560subStringSumK;

import java.util.HashMap;

public class Solution {
    public static int subarraySum(int[] nums, int k) {

        int count = 0;

        HashMap<Integer, Integer> countMap = new HashMap<>();
        countMap.put(0, 1);

        int prefixSum = 0;

        for (int i = 0; i < nums.length; i++) {
            prefixSum+= nums[i];

            if(countMap.containsKey(prefixSum-k)){
                count += countMap.get(prefixSum-k);
            }
            countMap.put(prefixSum, countMap.getOrDefault(prefixSum, 0) + 1);
        }
        return count;
    }

    public static void main(String[] args) {
        int[] sum ={1,-1,0};
        int i = subarraySum(sum, 0);
        System.out.println(i);
    }

}
